package org.example;

import org.example.entity.Student;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;

public class Main {
    public static void main(String[] args) throws IOException {
//        studentSort();
        copyString();
    }

    public static void copyString() throws IOException {
        BufferedWriter bw = new BufferedWriter(new FileWriter(new File("msg.txt"), true));
        String msg = "In a small village, a little boy lived with his father and mother. " +
                "He was the only son for them. The parents of the little boy were very depressed " +
                "due to his bad temper. The boy used to get angry very soon and taunt others " +
                "with his words. His bad temper made him fall for angry words. He scolded kids," +
                " neighbours and even his friends due to anger. He invited all worries for his parents " +
                "through the verbal usage. While he forgot what he spoke in anger, his friends and neighbours avoide";
        String[] arr = msg.split("\\.");
        Map<String, Integer> map = new HashMap<>();
        for (String str : arr) {
            char[] chars = str.toCharArray();
            for (char s : chars) {
                if (map.get(String.valueOf(s)) == null) {
                    map.put(String.valueOf(s), 1);
                } else {
                    map.put(String.valueOf(s), map.get(String.valueOf(s)) + 1);
                }
            }
            bw.write(str);
            bw.newLine();
            bw.flush();
        }
        for (String str : map.keySet()) {
            System.out.println(str + "出现了" + map.get(str) + "次");
        }
        bw.close();
    }

    private static void studentSort() throws IOException {
        List<Student> list = new ArrayList<>();
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < 5; i++) {
            System.out.println("请输入name,语文成绩,数学成绩,英语成绩:");
            String stu = sc.nextLine();
            String[] arr = stu.split(",");
            String name = arr[0];
            Integer chinese = Integer.parseInt(arr[1]);
            Integer math = Integer.parseInt(arr[2]);
            Integer english = Integer.parseInt(arr[3]);
            list.add(new Student(name, chinese, math, english));
        }
        BufferedWriter bw = new BufferedWriter(new FileWriter(new File("stu.txt"), true));
        list.sort(new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
                if (o1.getSum() < o2.getSum())
                    return 0;
                else
                    return -1;
            }
        });
        for (Student stu : list) {
            bw.write(stu.getName() + " " + stu.getChinese() + " " + stu.getMath() + " " + stu.getEnglish() + " " + stu.getSum());
            bw.newLine();
        }
        bw.close();
    }

}